<?php
/**
 * 将权限管理移到左侧菜单栏
 * 方法：添加一个二级分组，把现有的二级菜单改为三级
 */

$host = '113.44.136.17';
$port = 3306;
$database = 'qiandao';
$username = 'qiandao';
$password = 'NGbks3HWx5PhSxDX';

try {
    $pdo = new PDO("mysql:host={$host};port={$port};dbname={$database};charset=utf8mb4", $username, $password);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
    echo "=== 将权限管理移到左侧菜单栏 ===\n\n";
    
    // 1. 插入一个新的二级菜单"权限配置"作为分组
    $stmt = $pdo->prepare("
        INSERT INTO system_menu (pid, title, icon, url, node, params, target, sort, status, create_at)
        VALUES (9, '权限配置', 'layui-icon layui-icon-set', '#', '', '', '_self', 100, 1, NOW())
    ");
    $stmt->execute();
    $newGroupId = $pdo->lastInsertId();
    
    echo "✓ 创建二级分组菜单：权限配置 (ID:{$newGroupId})\n";
    
    // 2. 将"系统权限管理"和"系统用户管理"的 pid 改为新分组的 ID
    $stmt = $pdo->prepare("UPDATE system_menu SET pid = ? WHERE id IN (10, 11)");
    $stmt->execute([$newGroupId]);
    
    echo "✓ 将系统权限管理和系统用户管理移到权限配置下\n";
    
    // 3. 验证结果
    echo "\n【验证结果】\n";
    
    $stmt = $pdo->query("
        SELECT id, pid, title, url
        FROM system_menu
        WHERE id = 9 OR pid = 9 OR pid IN (SELECT id FROM system_menu WHERE pid = 9)
        ORDER BY 
            CASE WHEN id = 9 THEN 0 WHEN pid = 9 THEN 1 ELSE 2 END,
            sort DESC, id ASC
    ");
    
    $menus = $stmt->fetchAll(PDO::FETCH_ASSOC);
    
    foreach ($menus as $menu) {
        if ($menu['pid'] == 0) {
            echo "{$menu['title']} (ID:{$menu['id']})\n";
        } elseif ($menu['pid'] == 9) {
            echo "  ├─ {$menu['title']} (ID:{$menu['id']}, URL:{$menu['url']})\n";
        } else {
            echo "  │  └─ {$menu['title']} (ID:{$menu['id']}, URL:{$menu['url']})\n";
        }
    }
    
    echo "\n✓ 完成！现在权限管理是三级菜单，会显示在左侧菜单栏\n";
    echo "\n请刷新后台页面（Ctrl+F5）查看效果\n";
    
} catch (PDOException $e) {
    echo "数据库错误: " . $e->getMessage() . "\n";
}

